package com.tianqu.tms.www.car.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarGateInfoDto;
import com.tianqu.tms.www.car.controller.CarSearchInoutResultController;
import com.tianqu.tms.www.car.viewhelper.CarSeniorSearchInputViewhelper;
/**
 * 导出excel表格
 * @author #107
 *
 */
public class CarSearchInoutExportResultAction extends TmsAction {

    public final static String PAGENO = "pageNo";
    public final static String SORTKEY = "sortKey";
    public final static String MAXINDEX = "maxIndex";
    public final static String ASC = "asc";

    private Logger logger = Logger.getLogger(CarSearchInoutExportResultAction.class);
    
    @Override
    protected ActionForward doAction(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response,
            UserContext uc) throws Exception {
        
        logger.debug(this.getClass().getName() + "#execute was called.");
        CarSeniorSearchInputViewhelper view = new CarSeniorSearchInputViewhelper();

        if (uc.getAttribute(CarInnerListTopAction.PAGENO) != null) {
			uc.setAttribute(CarInnerListTopAction.PAGENO, "1");
		}
		if (uc.getAttribute(CarInnerListTopAction.MAXINDEX) != null) {
			uc.setAttribute(CarInnerListTopAction.MAXINDEX, "65535");
		}
        CarSearchInoutResultController controller = new CarSearchInoutResultController();
        controller.execute(uc);
        List list = (List)uc.getAttribute(TmsConstants.VIEWHELP);

        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("车辆进出查询结果");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        style.setBorderBottom((short) 1);   
        style.setBorderTop((short) 1);
        style.setBorderLeft((short) 1);
        style.setBorderRight((short) 1); 
        
        sheet.setDefaultRowHeightInPoints(10);
        sheet.setDefaultColumnWidth(17);
        
        HSSFFont  font  =  wb.createFont(); 
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        font.setFontHeightInPoints((short)8);
        style.setFont(font);
          
        HSSFCellStyle cellStyle = wb.createCellStyle();  
        HSSFFont titleFont = wb.createFont();
        titleFont.setFontName("宋体");
        titleFont.setFontHeight((short) 200);
        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        
        cellStyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        cellStyle.setBorderBottom((short) 1);   
        cellStyle.setBorderTop((short) 1);
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setBorderRight((short) 1); 
        cellStyle.setFont(titleFont);
        HSSFCell cell = row.createCell((short) 0);   
        cell.setCellValue("序号");  
        cell.setCellStyle(cellStyle);  

        HSSFCell cell1 = row.createCell((short) 1);  
        cell1.setCellValue("车辆编号");  
        cell1.setCellStyle(cellStyle); 
        
        HSSFCell cell2 = row.createCell((short) 2);  
        cell2.setCellValue("商户名称");  
        cell2.setCellStyle(cellStyle); 
        
        HSSFCell cell3 = row.createCell((short) 3);  
        cell3.setCellValue("道闸名称");  
        cell3.setCellStyle(cellStyle); 
        
        HSSFCell cell4 = row.createCell((short) 4);  
        cell4.setCellValue("识别方式");  
        cell4.setCellStyle(cellStyle); 
        
        HSSFCell cell5 = row.createCell((short) 5);  
        cell5.setCellValue("车牌号码");  
        cell5.setCellStyle(cellStyle); 
        
        HSSFCell cell6 = row.createCell((short) 6);  
        cell6.setCellValue("进出时间");  
        cell6.setCellStyle(cellStyle); 
        
        HSSFCell cell7 = row.createCell((short) 7);  
        cell7.setCellValue("进出状态");  
        cell7.setCellStyle(cellStyle); 

        HSSFCell cell8 = row.createCell((short) 8);  
        cell8.setCellValue("车身颜色");  
        cell8.setCellStyle(cellStyle); 

        HSSFCell cell9 = row.createCell((short) 9);  
        cell9.setCellValue("车牌颜色");  
        cell9.setCellStyle(cellStyle);

        HSSFCell cell10 = row.createCell((short) 10);  
        cell10.setCellValue("过滤状态");  
        cell10.setCellStyle(cellStyle);
        
        for(int i=1;i<list.size()+1;i++)
        {
            MarketCarGateInfoDto dto = (MarketCarGateInfoDto) list.get(i-1);
            row = sheet.createRow(i);
            HSSFCell c0 = row.createCell((short) 0);
            c0.setCellStyle(style);
            c0.setCellValue(i);  
            HSSFCell c1 = row.createCell((short) 1);
            c1.setCellStyle(style);
            c1.setCellValue(dto.getSerial()); 
            HSSFCell c2 = row.createCell((short) 2);
            c2.setCellStyle(style);
            c2.setCellValue(dto.getMemberName());
            HSSFCell c3 = row.createCell((short) 3);
            c3.setCellStyle(style);
            c3.setCellValue(dto.getGateType());
            HSSFCell c4 = row.createCell((short) 4);
            c4.setCellStyle(style);
            c4.setCellValue(dto.getThreadTypeInfo());
            HSSFCell c5 = row.createCell((short) 5);
            c5.setCellStyle(style);
            c5.setCellValue(dto.getLicensePlate());
            HSSFCell c6 = row.createCell((short) 6);
            c6.setCellStyle(style);
            c6.setCellValue(DateUtil.getTimeByTimeStamp(dto.getInsertTime()));
            HSSFCell c7 = row.createCell((short) 7);
            c7.setCellStyle(style);
            c7.setCellValue(dto.getThreadStatusInfo());
            HSSFCell c8 = row.createCell((short) 8);
            c8.setCellStyle(style);
            c8.setCellValue(dto.getCarBodyColor());
            HSSFCell c9 = row.createCell((short) 9);
            c9.setCellStyle(style);
            c9.setCellValue(dto.getCarLicenseColor());
            HSSFCell c10 = row.createCell((short) 10);
            c10.setCellStyle(style);
            c10.setCellValue(dto.getCheckFlag()==1?"OFF":"ON");
        }
        try  
        {  
            File file =new File(toUtf8String("车辆进出查询结果"+DateUtil.getCurrentTimeStr()+".xls"));
            OutputStream  fout = new FileOutputStream(file);  
            wb.write(fout); 
            getResponse(file, response);
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        } 
//        uc.setAttribute("tsxx", "1");
        request.setAttribute(TmsConstants.VIEWHELP, view);
        return null;
    }
}
